import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/el-form.vue"; //弹框form
import {
    getInspectionFormByFid, //详情
    saveInspectionForm, //报验-保存
    saveAuditInspectionForm,//总工审核-保存
    saveJlInspectionForm, //专监审核-保存
    getBaseDepartmentUser, //调监理
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: "材料报验单详情",
            isLock: false, //保存按钮是否可编辑
            dialogHeight: null,
            closeFormData: {
                fId: "", // 申请单fid //为空判断为新增
                fRequestcontent: "", //报验申请
                fRequestuserid: "", //技术负责人id
                fRequestusername: "", //技术负责人
                fRequestdates: "", //报验时间日期
                fAudituserid: "", //提请总工审核人id
                fAuditusername: "", //提请总工审核人

                fAuditpass: "",// 总工是否同意 "同意" "不同意"
                fAuditcontent: "",//总共意见
                fAuditdates: "",//总工审批日期
                fJluserid: "", //提请专监审核人id
                fJlusername: "", //提请专监审核人

                fJlpass: "", //专监是否同意 "同意" "不同意"
                fJlcontent: "", //专监审核意见
                fJldates: "" //审核日期
            },

            closeFormObj: formJs.closeFormObj, //报验信息
            ChiefEngineerFormObj: formJs.ChiefEngineerFormObj,//总工审核
            ChiefEngineer: false,//总工审核是否显示
            Faudit: formJs.Faudit, //专监审核
            examine: false, //专监审核是否展示

            btnShow: true,
            newRow: null,
            timeYMD: null, //当前日期
            timeYmdHms: null,
            JlusernameList: [], //监理用户列表初始值

        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },

        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                this.newRow = newVal;
            },
            deep: true,
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        this.getDetail(this.newRow);
        this.getHeight();
        this.getData();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            // console.log("selectChange", data, a, b);

            // 总工--是否同意
            if (b == "fAuditpass") {
                if (data == "不同意") {
                    this.ChiefEngineerFormObj[3].disabled = true;
                    this.ChiefEngineerFormObj[3].rules[0].required = false;
                    this.closeFormData.fJlusername = "";
                    this.closeFormData.fJluserid = "";
                } else {
                    this.ChiefEngineerFormObj[3].disabled = false;
                    this.ChiefEngineerFormObj[3].rules[0].required = true;
                }
            }

            // 提请总工审核人
            if (b == "fAuditusername") {
                this.JlusernameList.map(item => {
                    if (data == item.fRealname) {
                        this.closeFormData.fAuditusername = item.fRealname;
                        this.closeFormData.fAudituserid = item.fUserid;
                    }
                })
            }

            // 提请专监审核人
            if (b == "fJlusername") {
                this.JlusernameList.map(item => {
                    if (data == item.fRealname) {
                        this.closeFormData.fJlusername = item.fRealname;
                        this.closeFormData.fJluserid = item.fUserid;
                    }
                })
            }
        },
        edit() {
            this.closeFormData = this.$options.data.call(this).closeFormData;
            this.btnShow = true;
            this.ChiefEngineer = true;
            this.examine = true;

            if (this.statusNum == 1) { //编辑
                this.ChiefEngineer = false;
                this.examine = false;
                this.closeFormObj.map(item => {
                    item.disabled = false;
                })
                this.ChiefEngineerFormObj.map(item => {
                    item.disabled = true;
                })
                this.Faudit.map(item => {
                    item.disabled = true;
                })
            } else if (this.statusNum == 888) { //总工审核
                this.examine = false;
                this.closeFormObj.map(item => {
                    item.disabled = true;
                })
                this.ChiefEngineerFormObj.map(item => {
                    item.disabled = false;
                })
                this.Faudit.map(item => {
                    item.disabled = true;
                })
            } else if (this.statusNum == 3) { //专监审核
                this.closeFormObj.map(item => {
                    item.disabled = true;
                })
                this.ChiefEngineerFormObj.map(item => {
                    item.disabled = true;
                })
                this.Faudit.map(item => {
                    item.disabled = false;
                })
            } else if (this.statusNum == 7) {
                this.btnShow = false;
                //查看
                this.closeFormObj.map(item => {
                    item.disabled = true;
                })
                this.ChiefEngineerFormObj.map(item => {
                    item.disabled = true;
                })
                this.Faudit.map(item => {
                    item.disabled = true;
                })
            }
        },

        // 详情
        getDetail(row) {
            this.loading = true;
            getInspectionFormByFid({
                fId: row.fId
            }).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    this.closeFormData = res.data.inspectionform;
                    if (this.statusNum == 1) {
                        this.getJlusername(this.closeFormData.fCompanyid);
                        //报验申请
                        if (this.closeFormData.fRequestcontent == null) {
                            this.closeFormData.fRequestcontent = ""
                        }
                        //技术负责人
                        if (this.closeFormData.fRequestuserid == null || this.closeFormData.fRequestusername == null || this.closeFormData.fRequestuserid == "" || this.closeFormData.fRequestusername == "") {
                            this.closeFormData.fRequestuserid = this.userInfo.userId;
                            this.closeFormData.fRequestusername = this.userInfo.realName;
                        }
                        //报验时间日期
                        if (this.closeFormData.fRequestdates == null || this.closeFormData.fRequestdates == "") {
                            this.closeFormData.fRequestdates = this.timeYMD;
                        }
                        //提请总工审核人
                        if (this.closeFormData.fAuditusername == null || this.closeFormData.fAudituserid == null) {
                            this.closeFormData.fAuditusername = "";
                            this.closeFormData.fAudituserid = "";
                        }
                    }
                    if (this.statusNum == 888) {
                        this.getJlusername(this.closeFormData.fJlid);
                        //总公审核意见
                        if (this.closeFormData.fAuditpass == null) {
                            this.closeFormData.fAuditpass = "同意"
                        }
                        if (this.closeFormData.fAuditcontent == null) {
                            this.closeFormData.fAuditcontent = ""
                        }
                        //审核日期
                        if (this.closeFormData.fAuditdates == null || this.closeFormData.fAuditdates == "") {
                            this.closeFormData.fAuditdates = this.timeYMD;
                        }
                        //提请专监审核人
                        if (this.closeFormData.fJlusername == null || this.closeFormData.fJluserid == null) {
                            this.closeFormData.fJlusername = "";
                            this.closeFormData.fJluserid = "";
                        }
                    }
                    if (this.statusNum == 3) {
                        //总公审核意见
                        if (this.closeFormData.fJlpass == null) {
                            this.closeFormData.fJlpass = "同意"
                        }
                        if (this.closeFormData.fJlcontent == null) {
                            this.closeFormData.fJlcontent = ""
                        }
                        //审核日期
                        if (this.closeFormData.fJldates == null || this.closeFormData.fJldates == "") {
                            this.closeFormData.fJldates = this.timeYMD;
                        }
                    }
                } else {
                    this.$message.error("列表数据获取失败", res.msg);
                    this.loading = false;
                }
            });
        },


        yj_saveForm(num) {
            var save = {};
            if (num == 1) {
                save = {
                    fId: this.closeFormData.fId, // 申请单fid //为空判断为新增
                    fRequestcontent: this.closeFormData.fRequestcontent, //报验申请
                    fRequestuserid: this.closeFormData.fRequestuserid, //技术负责人id
                    fRequestusername: this.closeFormData.fRequestusername, //技术负责人
                    fRequestdates: this.closeFormData.fRequestdates, //报验时间日期*
                    fAudituserid: this.closeFormData.fAudituserid, //提请专监审核人id
                    fAuditusername: this.closeFormData.fAuditusername, //提请专监审核人
                    userId: this.userInfo.userId, // 当前登录人id
                    userName: this.userInfo.realName, // 当前登录人姓名
                };
                if (this.$refs.publicForm0.submitForm()) {
                    this.saveForm(save, num);
                } else {
                    console.log("error");
                }
            } else if (num == 888) {
                save = {
                    fId: this.closeFormData.fId, // 申请单fid //为空判断为新增
                    fAuditpass: this.closeFormData.fAuditpass, // 总工是否同意 "同意" "不同意"
                    fAuditcontent: this.closeFormData.fAuditcontent, //总工意见
                    fAuditdates: this.closeFormData.fAuditdates, // 总工审批日期
                    fJluserid: this.closeFormData.fJluserid, //提请监理id
                    fJlusername: this.closeFormData.fJlusername, //提请监理name
                    userId: this.userInfo.userId, // 当前登录人id
                    userName: this.userInfo.realName, // 当前登录人姓名
                };
                if (this.$refs.publicForm1.submitForm()) {
                    this.saveForm(save, num);
                } else {
                    console.log("error");
                }
            } else if (num == 3) {
                save = {
                    fId: this.closeFormData.fId, // 申请单fid //为空判断为新增
                    fJlpass: this.closeFormData.fJlpass, //专监是否同意 "同意" "不同意"
                    fJlcontent: this.closeFormData.fJlcontent, //专监审核意见
                    fJldates: this.closeFormData.fJldates, //审核日期
                    userId: this.userInfo.userId, // 当前登录人id
                    userName: this.userInfo.realName, // 当前登录人姓名
                };
                if (this.$refs.publicForm1.submitForm()) {
                    this.saveForm(save, num);
                } else {
                    console.log("error");
                }
            }

        },
        // 保存
        saveForm(obj, num) {
            this.loading = true;
            this.isLock = true;
            if (num == 1) {
                saveInspectionForm(obj)
                    .then((res) => {
                        if (res.errcode == 0) {
                            this.loading = false;

                            this.visible = false;
                            this.$emit("refreshList");
                            this.$message.success("保存成功");
                            this.isLock = false
                        } else {
                            this.loading = false;
                            this.isLock = false
                            this.$message.error(res.msg);
                            console.log("报验保存接口报错");
                        }
                    })
                    .catch((error) => {
                        console.log("报验保存接口", error);
                    });
            } else if (num == 888) {
                saveAuditInspectionForm(obj)
                    .then((res) => {
                        if (res.errcode == 0) {
                            this.visible = false;
                            this.$emit("refreshList");
                            this.$message.success("审核成功");
                            this.isLock = false
                        } else {
                            this.isLock = false
                            this.$message.error(res.msg);
                            console.log("专监审核接口报错");
                        }
                    })
                    .catch((error) => {
                        console.log("专监审核接口报错", error);
                    });
            } else if (num == 3) {
                saveJlInspectionForm(obj)
                    .then((res) => {
                        if (res.errcode == 0) {
                            this.visible = false;
                            this.$emit("refreshList");
                            this.$message.success("审核成功");
                            this.isLock = false
                        } else {
                            this.isLock = false
                            this.$message.error(res.msg);
                            console.log("专监审核接口报错");
                        }
                    })
                    .catch((error) => {
                        console.log("专监审核接口报错", error);
                    });
            }

        },
        // 监理
        getJlusername(id) {
            this.JlusernameList = []; //监理用户列表初始值
            var getSupervisionUserList = []; //监理用户列表过滤后
            this.closeFormObj[3].options = [];//总工
            this.ChiefEngineerFormObj[3].options = [];//专监

            getBaseDepartmentUser({
                fCompanyid: id
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        var getSupervisionUser = {
                            value: "",
                            label: "",
                        };
                        res.data.childList[0].personList.map((v) => {
                            this.JlusernameList.push(v);
                        });

                        this.JlusernameList.map((item) => {
                            getSupervisionUser = {
                                value: item.fRealname,
                                label: item.fRealname,
                            };
                            getSupervisionUserList.push(getSupervisionUser);
                        });
                        if (this.statusNum == 1) {
                            //总工
                            this.closeFormObj[3].options = getSupervisionUserList;
                        }
                        if (this.statusNum == 888) {
                            //专监
                            this.ChiefEngineerFormObj[3].options = getSupervisionUserList;
                        }
                    } else {
                        this.$message.error(res.msg);
                        console.log("部门管理-查询部门列表以及下属人员,接口报错");
                    }
                })
                .catch((error) => {
                    console.log("部门管理-查询部门列表以及下属人员", error.msg);
                });
        },
        // 时间
        getData() {
            var time = new Date();
            const y = time.getFullYear();
            const m = (time.getMonth() + 1 + "").padStart(2, "0");
            const d = (time.getDate() + "").padStart(2, "0");
            const hh = (time.getHours() + "").padStart(2, "0");
            const mm = (time.getMinutes() + "").padStart(2, "0");
            const ss = (time.getSeconds() + "").padStart(2, "0");
            this.timeYMD = `${y}-${m}-${d}`;
            this.timeYmdHms = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        },
    },
};